package com.cosmoplat.xikai.business.controller.scada;

import com.cosmoplat.xikai.business.domain.NumberOfProductionRecords;
import com.cosmoplat.xikai.business.domain.dto.scada.DeviceFaultDto;
import com.cosmoplat.xikai.business.enums.DeviceParamsEnum;
import com.cosmoplat.xikai.business.service.BaseDispatchTaskService;
import com.cosmoplat.xikai.business.service.BaseProductInfoService;
import com.cosmoplat.xikai.business.service.DeviceCallService;
import com.cosmoplat.xikai.common.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceCallService deviceCallService;
    @Autowired
    private BaseProductInfoService baseProductInfoService;
    @Autowired
    private BaseDispatchTaskService baseDispatchTaskService;
    @Value("${device.code}")
    private String deviceCode;


    /**
     * 获取实时扭矩
     *
     * @return
     */
    @ApiOperation(value = "获取实时扭矩与压力")
    @GetMapping("/obtainTorqueAndPressure")
    public AjaxResult obtainTorqueAndPressure() {
        List<String> identifierList = Arrays.asList(DeviceParamsEnum.CURRENT_MAXIMUM_MOMENT.getName(), DeviceParamsEnum.MAXIMUM_WORKING_MONMENT1.getName(), DeviceParamsEnum.MAXIMUM_WORKING_MONMENT2.getName());
        HashMap<String, String> map = new HashMap<>();
        try {
            map = deviceCallService.getCommandReturnValueMap(deviceCode, identifierList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (map.isEmpty()) {
            map.put(DeviceParamsEnum.CURRENT_MAXIMUM_MOMENT.getName(), "0");
            map.put(DeviceParamsEnum.MAXIMUM_WORKING_MONMENT1.getName(), "0");
            map.put(DeviceParamsEnum.MAXIMUM_WORKING_MONMENT2.getName(), "0");
        }
        return AjaxResult.success(map);
    }
    /**
     * 获取实时扭矩
     *
     * @return
     */
    @ApiOperation(value = "获取实时扭矩与压力")
    @GetMapping("/getTorqueAndPressure")
    @PreAuthorize("@ss.hasPermi('device:getTorqueAndPressure')")
    public AjaxResult getTorqueAndPressure(DeviceFaultDto deviceFaultDto) {
        return deviceCallService.getTorqueAndPressure( deviceFaultDto);
    }

    /**
     * 获取生产记录总数
     *
     * @return
     */
    @ApiOperation(value = "获取生产记录的总数")
    @GetMapping("/getsTheTotalNumberOfProductionRecords")
    public AjaxResult getsTheTotalNumberOfProductionRecords() {
        List<NumberOfProductionRecords> list = baseProductInfoService.queryNumberOfProductionRecords();
        return AjaxResult.success(list);
    }


    /**
     * 获取实际生产计划
     *
     * @param deviceFaultDto
     * @return
     */
    @ApiOperation(value = "获取实际生产计划")
    @GetMapping("/actual/production/planning")
    public AjaxResult actualProductionPlanning(DeviceFaultDto deviceFaultDto) {
        return AjaxResult.success(baseDispatchTaskService.actualProductionPlanning(deviceFaultDto));
    }


    /**
     * 获取产量
     *
     * @param deviceFaultDto
     * @return
     */
    @ApiOperation(value = "获取产量")
    @GetMapping("/yield/acquisition")
    public AjaxResult yieldAcquisition(DeviceFaultDto deviceFaultDto) {
        return AjaxResult.success(baseDispatchTaskService.yieldAcquisition(deviceFaultDto));
    }
}
